<?php

declare(strict_types=1);
/**
 * This file is part of bearing-web.
 *
 * @link     https://www.bearing.cn/
 * @contact  tech@bearing.cn
 * @license  https://gitee.com/cppo/bearing_web/-/blob/master/README.md
 */

namespace common\models;

use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\db\BaseActiveRecord;
use yii\web\IdentityInterface;

/**
 * This is the model class for table "{{%user}}".
 *
 * @property int $id
 * @property string $name
 * @property string $password
 * @property string $email
 * @property int $create_time
 * @property int $update_time
 */
class UserModel extends ActiveRecord implements IdentityInterface
{
    public static function tableName(): string
    {
        return '{{%user}}';
    }

    public function rules(): array
    {
        return [
            [['create_time', 'update_time'], 'default', 'value' => 0],
            [['name', 'password', 'email'], 'required'],
            [['create_time', 'update_time'], 'integer'],
            [['name', 'email'], 'string', 'max' => 255],
            [['password'], 'string', 'max' => 32],
            [['email'], 'unique'],
        ];
    }

    public function attributeLabels(): array
    {
        return [
            'id' => 'ID',
            'name' => 'Name',
            'password' => 'Password',
            'email' => 'Email',
            'create_time' => 'Create Time',
            'update_time' => 'Update Time',
        ];
    }

    /**
     * 时间更新策略.
     *
     * @return array[]
     */
    public function behaviors(): array
    {
        return [
            [
                'class' => TimestampBehavior::class,
                'attributes' => [
                    BaseActiveRecord::EVENT_BEFORE_INSERT => ['create_time', 'update_time'],   // 创建
                    BaseActiveRecord::EVENT_BEFORE_UPDATE => ['update_time'],       // 修改
                ],
                'value' => function () {
                    return time();
                },
            ],
        ];
    }

    /**
     * 根据给到的ID查询身份。
     *
     * @param mixed $id
     * @return null|UserModel 通过ID匹配到的身份对象
     */
    public static function findIdentity($id): ?UserModel
    {
        return static::findOne($id);
    }

    public function getId(): int
    {
        return $this->getPrimaryKey();
    }

    public static function findByEmail(string $email): ?self
    {
        return self::findOne(['email' => $email]);
    }

    public function validatePassword(string $password): bool
    {
        return $this->password === md5($password);
    }

    public function setPassword(string $password): void
    {
        $this->password = md5($password);
    }

    public static function findIdentityByAccessToken($token, $type = null): null|IdentityInterface|UserModel
    {
        if (empty($token)) {
            return null;
        }
        $obj = UserTokenModel::findOne(['token' => $token]);
        if (! $obj instanceof UserTokenModel || ! $obj->id || $obj->expire_time < time()) {
            return null;
        }
        return self::findOne($obj->user_id);
    }

    public function getAuthKey()
    {
        return null;
    }

    public function validateAuthKey($authKey): bool
    {
        return true;
    }
}
